﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PhotoHsn.WebApp.Core;
using PhotoHsn.BLL.Core;
using PhotoHsn.BLL.Adapters;
using PhotoHsn.BLL.Authantication;
using Bxas.Core.Exceptions;
using System.Diagnostics;

namespace PhotoHsn.WebApp
{
    public partial class NewUser1 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                load_ddlSecQues();
            }
        }

        private void load_ddlSecQues()
        {
            SecurityQuestionsBs scbs = new SecurityQuestionsBs();
            try
            {
                List<SecurityQuestions> list = scbs.get_all_actives();
                ddlSecQues.DataSource = list;
                ddlSecQues.DataTextField = "Text";
                ddlSecQues.DataValueField = "Id";
                ddlSecQues.DataBind();
                ddlSecQues.Items.Insert(0, new ListItem("Seçiniz", ""));
            }
            catch (BxasCoreException ex)
            {
                MessageShow(ErrorLogInsert(ex, this, new StackFrame()), EnumMessageTypes.Hata);            
            }

        }

        protected void lnkBtnSubmit_Click(object sender, EventArgs e)
        {
            MembersBs membersBs = new MembersBs();
            if (wucCapcha1.IsValid)
            { 
                try
                {
                    Boolean DoesUserName = membersBs.get_for_UserNameControl(txtUserName.Text);
                    Boolean DoesEmail = membersBs.get_for_EmailControl(txtEMail.Text);

                    if (!DoesEmail && !DoesUserName)
                    {
                        Members member = new Members();
                        member.UserName = txtUserName.Text;
                        member.Password = Bxas.Utility.Security.MD5Operations.convert_string_to_MD5(txtPassword.Text);
                        member.CreateDate = DateTime.Now;
                        member.Email = txtEMail.Text;
                        member.FirstName = txtName.Text;
                        member.MiddleName = txtMiddelName.Text;
                        member.LastName = txtLastName.Text;
                        member.Gender = Convert.ToBoolean(Convert.ToInt32(rblGender.SelectedValue));
                        member.Id = Guid.NewGuid();
                        member.IsDeleted = false;
                        member.IsOnline = false;
                        member.IsStatus = EnumMemberStatus.Passive;
                        member.SecurityQuestionId = new Guid(ddlSecQues.SelectedValue);
                        member.SecurityResponse = txtAnswer.Text;
                        member.WrongPasswordCount = 0;
                        membersBs.insert(member);
                        //Email gönderme metodu kullanılacak burada kullanıcıya email atılması gerekiyor
                        MessageShow("Üye kaydınız yapıldı. Lütfen Email adresinize gelen aktivasyon linkini kullanarak üyeliğinizi aktif hale getirin.", EnumMessageTypes.Bilgi);
                    }
                    else
                    {                      
                            MessageShow((DoesUserName?"Kullanıcı Adı Kullanılmakta.\n":"")+(DoesEmail?"Email Kullanılmakta.":""), EnumMessageTypes.Uyari);                                                  
                    }

                }
                catch (Exception ex)
                {                   
                    MessageShow(ErrorLogInsert(ex, this, new StackFrame()), EnumMessageTypes.Hata);
                }                

            }
            else
            {
                MessageShow("Seçtiğiniz nesne ile sorulan nesne uyuşmamaktadır.", EnumMessageTypes.Uyari);
            }
        }
    }
}